﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarolLib.Control;
using DispatchAssist.Weather.Controls;
using DispatchAssist.Weather.Interfaces;
using DispatchAssist.Weather.Entities;
using DispatchAssist.Weather.Helper;

namespace DispatchAssist.Weather
{
    [ModuleContract("419CF187-429F-42EE-AA4D-D51E2FF36870", "天气管理", "天气管理")]
    public partial class WeatherManage : MdiUserControl
    {
        internal IWeatherMapProvider ActiveMapProvider { get; private set; }

        internal IWeatherDisplayProvider ActiveDisplayProvider { get; private set; }

        public WeatherManage()
        {

            log4net.Config.XmlConfigurator.Configure();
            InitializeComponent();
        }



        private void WeatherManage_Load(object sender, EventArgs e)
        {
            ActiveDisplayProvider = ProviderHelper.GetWeatherProvider();
            if (ActiveDisplayProvider != null)
            {
                ActiveMapProvider = ActiveDisplayProvider.WeatherMapProvider;
                LoadProviderTreeNodes(ActiveMapProvider);
                ActiveDisplayProvider.DisplayWeatherMap += new DisplayWeatherMapEventHandler(ActiveDisplayProvider_DisplayWeatherMap);
            }

            cbAnimateTime.DisplayMember = "Text";
            cbAnimateTime.ValueMember = "AnimateTime";
            cbAnimateTime.DataSource = ProviderHelper.GetAnimateDataSource();
            cbAnimateTime.SelectedIndex = 0;
        }

        private void ActiveDisplayProvider_DisplayWeatherMap(IWeatherMapInfo mapInfo, Image image)
        {
            this.BeginInvoke(new Action(() =>
            {
                try
                {
                    if (tvWeatherMapType.SelectedNode != null && mapInfo == tvWeatherMapType.SelectedNode.Tag as IWeatherMapInfo)
                    {
                        pbWeather.Image = image;
                        pbLoading.Visible = false;
                    }
                }
                catch
                {
                }
            }));
        }

        private void LoadAirportDatas()
        {

        }
        /// <summary>
        /// 加载数据源中的点节
        /// </summary>
        private void LoadProviderTreeNodes(IWeatherMapProvider mapProvider)
        {
            tvWeatherMapType.Nodes.Clear();

            if (mapProvider == null)
                return;

            foreach (var type in mapProvider.Types)
            {
                WeatherMapTypeTreeNode.AddChildTypes(this.tvWeatherMapType, type);
            }
        }
        /// <summary>
        /// 加载收藏夹
        /// </summary>
        /// <param name="provider"></param>
        /// <returns></returns>
        private void LoadFavoriteTreeNodes(IWeatherDisplayProvider provider, IList<IWeatherMapInfo> favoriteTypeInfos)
        {
            var favoriteParentNode = new FavoriteTreeNode("收藏夹", null);
            this.tvWeatherMapType.Nodes.Add(favoriteParentNode);

            if (favoriteTypeInfos == null)
                return;

            foreach (var info in favoriteTypeInfos)
            {
                favoriteParentNode.AddChild(info);
            }
        }

        private void btnDataManage_Click(object sender, EventArgs e)
        {
            try
            {
                var form = new frmDataManage();
                form.ShowDialog();

                LoadProviderTreeNodes(ActiveMapProvider);
            }
            catch (Exception ex)
            {
                CarolLib.Log.Instance.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            try
            {
                var form = new frmWeatherSetting();
                form.ShowDialog();

                var provider = ProviderHelper.GetWeatherProvider();
                if (ActiveDisplayProvider != provider)
                {
                    ActiveDisplayProvider.DisplayWeatherMap -= new DisplayWeatherMapEventHandler(ActiveDisplayProvider_DisplayWeatherMap);

                    ActiveDisplayProvider = provider;
                    ActiveMapProvider = ActiveDisplayProvider.WeatherMapProvider;
                    LoadProviderTreeNodes(ActiveMapProvider);
                    ActiveDisplayProvider.DisplayWeatherMap += new DisplayWeatherMapEventHandler(ActiveDisplayProvider_DisplayWeatherMap);
                }
            }
            catch (Exception ex)
            {
                CarolLib.Log.Instance.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void tvWeatherMapType_AfterSelect(object sender, TreeViewEventArgs e)
        {

            var mapInfo = e.Node.Tag as IWeatherMapInfo;
            if (mapInfo == null)
                return;
            try
            {
                if (e.Node is WeatherMapTreeNode)
                {
                    ActiveDisplayProvider.SelecteWeatherMapInfo(mapInfo);
                    ActiveDisplayProvider.Show();
                }
            }
            catch (Exception ex)
            {
                CarolLib.Log.Instance.Error(ex);
            }
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            if (ActiveDisplayProvider != null)
            {
                if (ActiveDisplayProvider.IsPlay)
                {
                    ActiveDisplayProvider.Suspend();
                    btnPlay.Text = "Play";
                    cbAnimateTime.Enabled = true;
                }
                else
                {
                    ActiveDisplayProvider.Play((double)cbAnimateTime.SelectedValue);
                    btnPlay.Text = "Stop";
                    cbAnimateTime.Enabled = false;
                }
            }
        }

        private void tvWeatherMapType_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node is WeatherMapTreeNode)
            {
                pbLoading.Visible = true;
            }
        }

        private void cbAnimateTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item = cbAnimateTime.SelectedItem as AnimateDisplayTime;
            if (item == null)
                return;

            this.btnPlay.Enabled = item.AnimateTime > 0;
        }

        private void pbWeather_Paint(object sender, PaintEventArgs e)
        {
            if (pbWeather.Image != null)
            {
                try
                {
                    var airports = AirportHelper.GetDisplayAirports(ActiveDisplayProvider.WeatherMapInfo);

                    var s = pbWeather.Width / (decimal)pbWeather.Height;
                    var zoomScale = 0m;
                    foreach (var airport in airports)
                    {
                        var s2 = pbWeather.Image.Width / (decimal)pbWeather.Image.Height;
                        var top = 0m;
                        var left = 0m;
                        if (s >= s2)
                        {//宽比高长，则
                            zoomScale = pbWeather.Height / (decimal)pbWeather.Image.Height;
                            left = (pbWeather.Width - zoomScale * pbWeather.Image.Width) / 2;
                        }
                        else
                        {
                            zoomScale = pbWeather.Width / (decimal)pbWeather.Image.Width;
                            top = (pbWeather.Height - zoomScale * pbWeather.Image.Height) / 2;
                        }
                        AirportHelper.DrawAirport(e, zoomScale, ActiveDisplayProvider.WeatherMapInfo, airport, left, top);
                    }
                }
                catch
                { }
            }



        }

    }
}
